package com.Interface.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Interface.dao.ClassInfoMapper;
import com.Interface.dao.NoticeInfoMapper;
import com.Interface.dao.TopicInfoMapper;
import com.Interface.dao.TopicMarkInfoMapper;
import com.Interface.dao.UserInfoMapper;
import com.Interface.entity.ClassInfo;
import com.Interface.entity.NoticeInfo;
import com.Interface.entity.TopicInfo;

@Service
public class TopicInfoService {

	@Autowired
	TopicInfoMapper topicDao;
	@Autowired
	UserInfoMapper userDao;
	@Autowired
	ClassInfoMapper classInfoDao;
	@Autowired
	NoticeInfoMapper noticeInfoDao;
	@Autowired
	TopicMarkInfoMapper topicMarkInfoDao;
	
	public List<TopicInfo> selectRecommend(Map<String, Object> param){
		if((int)param.get("type")==0)
			return topicDao.selectRecommend(param);
		else if((int)param.get("type")==1)
			return topicDao.selectFlow(param);
		else if((int)param.get("type")==2)
			return topicDao.selectHot(param);
		else if((int)param.get("type")==3)
			return topicDao.selectNew(param);		
		return null;
	}
	public List<TopicInfo> selectClassTopicPreview(Map<String, Object> param){
		return topicDao.selectClassTopicPreview(param);
	}
	public List<TopicInfo> selectUserTopicPreview(Map<String, Object> param){
		return topicDao.selectUserTopicPreview(param);
	}
	public List<TopicInfo> selectUserCollectionTopicPreview(Map<String, Object> param){
		return topicDao.selectUserCollectionTopicPreview(param);
	}
	 /*public List<Map<String, Object>>selectByKeyWord(Map<String, Object> param){
		 return topicDao.selectByKeyWord(param);
	 }*/
	public List<TopicInfo> selectByKeyWord(Map<String, Object> param){
		return topicDao.selectByKeyWord(param);
	}
	 public Map<String, Object>selectByTopicId(Map<String, Object> param){
		 return topicDao.selectByTopicId(param);
	 }
	 
	 public int updateTopicReadCnt(int topicId){
		 return topicDao.updateTopicReadCnt(topicId);
	 }
	 
	 public int insertSelective(TopicInfo record){
		 return topicDao.insertSelective(record);
	 }
	 
	 public int insertTopic(TopicInfo record){
		 int rlt = topicDao.insertSelective(record);
		 if(rlt==1){
			 rlt = userDao.addTopicCntByUserId(record.getUserId());	
			 rlt = classInfoDao.addClassTopicCnt(record.getClassId());
			 NoticeInfo noticeInfo = new NoticeInfo();
			 noticeInfo.setCreateTime(new Date());
			 noticeInfo.setNoticeType(0);
			 noticeInfo.setIsAlert(1);
			 ClassInfo classInfo= classInfoDao.selectByPrimaryKey(record.getClassId()); 
			 noticeInfo.setNoticeUserId(classInfo.getCreateUserId());
			 noticeInfo.setTopicId(record.getId());
			 if(classInfo.getIsTopicNotice()==0){
				 noticeInfoDao.insertSelective(noticeInfo);
			 }
		 }
		 return rlt;
	 }
	 /*public List<Map<String, Object>> getHotActivity(Map<String, Object> param){
		 return topicDao.getHotActivity(param);
	 }*/
	 public List<TopicInfo> getHotActivity(Map<String, Object> param){
		 return topicDao.getHotActivity(param);
	 }
	 
	 
	 public Map<String, Object> getVoteLimit(int topicId){
		 return topicDao.getVoteLimit(topicId);
	 }
	 
	 public int getCountByKeyWord(Map<String, Object> param){
		 return topicDao.getCountByKeyWord(param);
	 }
	 
	 public int deleteTopic(Map<String, Object> param){
		 int classid = topicDao.getTopicClass(param);
		 int rlt = topicDao.deleteTopic(param);
		 if(rlt==1)
		 {
			 userDao.reduceTopicCntByUserId(Integer.parseInt(param.get("user_id").toString()));
			 classInfoDao.reduceClassTopicCnt(classid);
			 topicMarkInfoDao.deleteTopicMark(Integer.parseInt(param.get("topic_id").toString()));
			 return 1;
		 }
		 return 0;
	 }
}
